<template>
  <div>
    <el-table
      :data="permissionArr"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="权限值" prop="code"></el-table-column>
      <el-table-column label="修改时间" prop="updateTime"></el-table-column>
      <el-table-column label="操作">
        <template #="{ row }">
          <el-button
            type="primary"
            size="small"
            @click="addPermission(row)"
            icon="Plus"
            :disabled="row.level === 4"
          >
            {{ row.level === 3 ? '添加功能' : '添加菜单' }}
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="updatePermission(row)"
            icon="Edit"
          >
            编辑
          </el-button>
          <el-popconfirm
            :title="`确认删除？${row.name}`"
            @confirm="removeMenu(row)"
          >
            <template #reference>
              <el-button
                type="primary"
                size="small"
                icon="Delete"
                :disabled="row.level === 1"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框组件：添加或更新已有菜单 -->
    <el-dialog
      :title="menuData.id ? '更新菜单' : '添加菜单'"
      v-model="dialogVisible"
      width="30%"
      @close=""
    >
      <!-- 表单组件用于收集新增和已有菜单的数据 -->
      <!-- <template #default > -->
      <el-form>
        <el-form-item label-width="60px" label="名称">
          <el-input
            placeholder="请你输入菜单的名称"
            v-model="menuData.name"
          ></el-input>
        </el-form-item>
        <el-form-item label-width="60px" label="权限值">
          <el-input
            placeholder="请你输入相应的权限值"
            v-model="menuData.code"
          ></el-input>
        </el-form-item>
      </el-form>

      <!-- </template> -->
      <template #footer>
        <el-button type="primary" size="default" @click="">取消</el-button>
        <el-button type="primary" size="default" @click="save">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue'
//引入请求API
import {
  reqAllPermisstion,
  reqAddOrUpdateMenu,
  reqRemoveMenu,
} from '@/apis/acl/menu/index'
//引入类型
import type {
  PermissionResponseData,
  PermissionList,
  Permission,
  MenuParams,
} from '@/apis/acl/menu/type'
import { ElMessage } from 'element-plus'
//树形数据
let permissionArr = ref<PermissionList>([])

//控制对话框显示与隐藏
let dialogVisible = ref<boolean>(false)

//携带的参数
let menuData = reactive<MenuParams>({
  code: '', //权限数值
  level: 0, //几级菜单
  name: '',
  pid: 0, //菜单id
})

//获取全部数据
const getHasPermisstion = async () => {
  let result: PermissionResponseData = await reqAllPermisstion()
  if (result.code === 200) {
    permissionArr.value = result.data
  }
}

//添加菜单按钮的回调
const addPermission = (row: Permission) => {
  //清空数据
  Object.assign(menuData, {
    id: 0,
    code: '', //权限数值
    level: 0, //几级菜单
    name: '',
    pid: 0, //菜单id
  })

  //对话框显示
  dialogVisible.value = true

  //收集新增的菜单Level数值
  menuData.level = row.level + 1
  //给谁新增子菜单
  menuData.pid = row.id as number
}

//确定按钮的回调
const save = async () => {
  //发请求：新增子菜单|更新某一个已有的菜单的数据
  let result: any = await reqAddOrUpdateMenu(menuData)
  if (result.code === 200) {
    //对话框隐藏
    dialogVisible.value = false
    //提示信息
    ElMessage({
      type: 'success',
      message: menuData.id ? '更新成功' : '添加成功',
    })
    //再次获取全部最新的菜单数据
    getHasPermisstion()
  }
}

//编辑已有的菜单
const updatePermission = (row: Permission) => {
  //编辑菜单
  dialogVisible.value = true
  //修改已有的菜单数据更新
  Object.assign(menuData, row)
}

//删除按钮的回调
const removeMenu = async (row: Permission) => {
  let result = await reqRemoveMenu(row.id)
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getHasPermisstion()
  }
}

//组件挂载完毕
onMounted(() => {
  getHasPermisstion()
})
</script>

<style lang="scss" scoped></style>
